<?php
namespace app\common;

use think\db\Query;
use think\Validate;

class RootService
{
    /**
     * 引入trait类
     */
    use RootAuth, RootEncrypt;

    /**
     * 异常提示
     * @param string $message
     * @param int $code
     * @throws RootException
     */
    protected function errorException(string $message, int $code = 1)
    {
        throw new RootException($message, $code);
    }

    /**
     * 分页
     * @param Query $query
     * @return array
     * @throws \think\db\exception\DbException
     */
    protected function page(Query $query, array $append = [])
    {
        return $query->paginate(request()->param('limit') ?: 10)->append($append)->toArray();
    }

    /**
     * 参数校验
     * @param Validate $validate
     * @param string $scene
     * @param array $params
     * @throws RootException
     */
    protected function validate(Validate $validate, string $scene, array $params = [])
    {
        $validate->scene($scene)->check($params ?: request()->param()) || $this->errorException($validate->getError());
    }
}